package com.pz.gulimall.auth.config;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.scheduling.annotation.AsyncConfigurer;
import org.springframework.scheduling.annotation.SchedulingConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskScheduler;
import org.springframework.scheduling.config.ScheduledTaskRegistrar;

import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

@Configuration
@RefreshScope
public class AsyncConfig {
    @Value("${gulimall.thread.core-size}")
    private int threadPoolCoreSize;
    @Value("${gulimall.thread.max-size}")
    private int threadPoolMaxSize;
    @Value("${gulimall.thread.keep-alive-time}")
    private long threadPoolKAT;

    @Bean("executor")
    public ThreadPoolExecutor initThreadPool(){
        return new ThreadPoolExecutor(threadPoolCoreSize, threadPoolMaxSize, threadPoolKAT,
                TimeUnit.SECONDS,
                new LinkedBlockingDeque<>(100000),
                Executors.defaultThreadFactory(),
                new ThreadPoolExecutor.AbortPolicy()
        );
    }
}
